package bb.lanxing.lib.devices.antplus;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import com.dsi.ant.plugins.antplus.pcc.defines.DeviceState;
import com.dsi.ant.plugins.antplus.pcc.defines.RequestAccessResult;
import com.dsi.ant.plugins.antplus.pccbase.AntPluginPcc;
import com.dsi.ant.plugins.antplus.pccbase.PccReleaseHandle;
import bb.lanxing.lib.devices.api.SmartDevice;
import bb.lanxing.lib.devices.core.AbstractPeerDevice;

public abstract class AbsAntPlusDevice<PPC extends AntPluginPcc> extends AbstractPeerDevice
        implements AntPluginPcc.IDeviceStateChangeReceiver, AntPluginPcc.IPluginAccessResultReceiver<PPC> {
    private static final String TAG = "ANT_PLUS<<-->>";
    private static final Handler mMainHandler = new Handler(Looper.getMainLooper());
    protected PPC mAntPcc;
    protected Context mAppContext;
    private int mCurrentState;
    private PccReleaseHandle<PPC> mPccReleaseHandle;

    @Override
    public int getDeviceState() {
        return 17;
    }

    protected abstract PccReleaseHandle<PPC> requestAccessToPPC();

    @Override
    public void setDisConnectFromUser(boolean z) {
    }

    protected void subscribeEvents(PPC ppc) {
    }

    protected void unsubscribeEvents(PPC ppc) {
    }

    public AbsAntPlusDevice(Context context, SmartDevice smartDevice) {
        super(smartDevice);
        this.mAppContext = context;
    }

    @Override
    public void connect() {
        String address = getAddress();
        if (TextUtils.isEmpty(address) || !TextUtils.isDigitsOnly(address)) {
            return;
        }
        d("try to connect to : " + getName());
        mMainHandler.post(new Runnable() {
            @Override
            public void run() {
                AbsAntPlusDevice absAntPlusDevice = AbsAntPlusDevice.this;
                absAntPlusDevice.mPccReleaseHandle = absAntPlusDevice.requestAccessToPPC();
            }
        });
    }

    @Override
    public void disconnect() {
        close();
    }

    @Override
    public void close() {
        d("close device");
        PPC ppc = this.mAntPcc;
        if (ppc != null) {
            unsubscribeEvents(ppc);
            this.mAntPcc.releaseAccess();
        } else {
            PccReleaseHandle<PPC> pccReleaseHandle = this.mPccReleaseHandle;
            if (pccReleaseHandle != null) {
                pccReleaseHandle.close();
            }
        }
        this.mPccReleaseHandle = null;
        this.mAntPcc = null;
    }

    @Override
    public boolean isConnected() {
        PPC ppc = this.mAntPcc;
        return ppc != null && convertState(ppc.getCurrentDeviceState()) == 2;
    }

    @Override
    public boolean isConnecting() {
        PPC ppc = this.mAntPcc;
        return ppc != null && convertState(ppc.getCurrentDeviceState()) == 1;
    }

    @Override
    public int getConnectionState() {
        PPC ppc = this.mAntPcc;
        if (ppc != null) {
            return convertState(ppc.getCurrentDeviceState());
        }
        return 4;
    }

    public int getDeviceNumber() {
        String address = getAddress();
        if (TextUtils.isEmpty(getAddress()) || TextUtils.isDigitsOnly(address)) {
            return 0;
        }
        return Integer.parseInt(address);
    }

    @Override
    public void onResultReceived(PPC ppc, RequestAccessResult requestAccessResult, DeviceState deviceState) {
        StringBuilder sb = new StringBuilder("onResultReceived: \n");
        sb.append("PPC:" + ppc);
        sb.append("\n");
        sb.append("RequestAccessResult: " + requestAccessResult);
        sb.append("\n");
        sb.append("DeviceState: " + deviceState);
        sb.append("\n");
        d(sb.toString());
        switch (requestAccessResult) {
            case SUCCESS -> {
                if (this.mAntPcc != null) {
                    close();
                }
                this.mAntPcc = ppc;
                subscribeEvents(ppc);
            }
            case USER_CANCELLED, CHANNEL_NOT_AVAILABLE, OTHER_FAILURE, DEPENDENCY_NOT_INSTALLED, DEVICE_ALREADY_IN_USE,
                    SEARCH_TIMEOUT, ALREADY_SUBSCRIBED, BAD_PARAMS, ADAPTER_NOT_DETECTED, UNRECOGNIZED -> unsubscribeEvents(ppc);
        }
        onDeviceStateChange(deviceState);
    }

    @Override
    public void onDeviceStateChange(DeviceState deviceState) {
        d("onDeviceStateChange: " + deviceState);
        int convertState = convertState(deviceState);
        if (this.mCurrentState == convertState) {
            return;
        }
        this.mCurrentState = convertState;
        notifyStateChanged(convertState, 0);
    }

    protected int convertState(DeviceState deviceState) {
        if (deviceState == DeviceState.TRACKING) {
            return 2;
        } else {
            return 4;
        }
//        return deviceState != DeviceState.TRACKING ? 4 : 2;
    }

    @Override
    protected String getTag() {
        return "ANT_PLUS<<-->>-" + getName();
    }
}
